package com.finance.web.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.finance.common.utils.DateUtils;
import com.finance.web.domain.FmAccount;
import com.finance.web.mapper.FmAccountMapper;
import org.apache.commons.math3.util.MathUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.finance.web.mapper.FmJournalMapper;
import com.finance.web.domain.FmJournal;
import com.finance.web.service.IFmJournalService;
import com.finance.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 日记账Service业务层处理
 *
 * @author lisen
 * @date 2021-11-11
 */
@Transactional
@Service
public class FmJournalServiceImpl implements IFmJournalService {
    @Autowired
    private FmJournalMapper fmJournalMapper;
    @Autowired
    private FmAccountMapper fmAccountMapper;

    /**
     * 查询日记账
     *
     * @param id 日记账主键
     * @return 日记账
     */
    @Override
    public FmJournal selectFmJournalById(Long id) {
        return fmJournalMapper.selectFmJournalById(id);
    }

    /**
     * 查询日记账列表
     *
     * @param fmJournal 日记账
     * @return 日记账
     */
    @Override
    public List<FmJournal> selectFmJournalList(FmJournal fmJournal) {
        return fmJournalMapper.selectFmJournalList(fmJournal);
    }

    /**
     * 新增日记账
     *
     * @param fmJournal 日记账
     * @return 结果
     */
    @Override
    public int insertFmJournal(FmJournal fmJournal) {

        BigDecimal balance = updateAccountBalanceByJournal(fmJournal);
        fmJournal.setBalance(balance);
        fmJournal.setCreateTime(DateUtils.getNowDate());
        return fmJournalMapper.insertFmJournal(fmJournal);
    }

    /**
     * 修改日记账
     *
     * @param fmJournal 日记账
     * @return 结果
     */
    @Override
    public int updateFmJournal(FmJournal fmJournal) {
        FmJournal oldJournal = fmJournalMapper.selectFmJournalById(fmJournal.getId());
        //先把之前的加回来
        oldJournal.setAmountType("1".equals(oldJournal.getAmountType())?"2":"1");
        updateAccountBalanceByJournal(oldJournal);
        //再修改
        BigDecimal balance = updateAccountBalanceByJournal(fmJournal);

        return fmJournalMapper.updateFmJournal(fmJournal);
    }

    /**
     * 批量删除日记账
     *
     * @param ids 需要删除的日记账主键
     * @return 结果
     */
    @Override
    public int deleteFmJournalByIds(String ids) {
        List<FmJournal> journalList = fmJournalMapper.selectFmJournalByIds(Convert.toStrArray(ids));
        for (FmJournal journal : journalList) {
            //翻转借贷方向，还原账户余额
            journal.setAmountType("1".equals(journal.getAmountType())?"2":"1");
            updateAccountBalanceByJournal(journal);
        }
        return fmJournalMapper.deleteFmJournalByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除日记账信息
     *
     * @param id 日记账主键
     * @return 结果
     */
    @Override
    public int deleteFmJournalById(Long id) {
        return fmJournalMapper.deleteFmJournalById(id);
    }


    /**
     * 根据日记账数据修改账户
     * @param fmJournal
     * @return
     */
    private BigDecimal updateAccountBalanceByJournal(FmJournal fmJournal) throws RuntimeException{
        BigDecimal amount = fmJournal.getAmount();
        FmAccount account = fmAccountMapper.selectFmAccountById(fmJournal.getAccountId());
        if(account==null){
            throw new RuntimeException("账户"+fmJournal.getAccountId()+"不存在");
        }
        BigDecimal balance=account.getBalance();

        if ("1".equals(fmJournal.getAmountType())) {
            balance = balance.add(amount);
        } else if ("2".equals(fmJournal.getAmountType())) {
            balance = balance.subtract(amount);
        }

        account.setBalance(balance);
        int ret= fmAccountMapper.updateFmAccountBalance(account);
        if(ret<=0){
            throw new RuntimeException("账户"+fmJournal.getAccountId()+"修改失败");
        }
        return balance;
    }
}
